#!/bin/python
import numpy as np
import matplotlib.pyplot as plt

def logf_scan(T_ms, n=4000000, m=125, M=255):
    i=1
    isInc=0
    t=0
    seq=[]
    PWM_MOS=M
    while T_ms!=0:
        #PWM变化方向
        if PWM_MOS<=m:
            isInc=1
        if PWM_MOS>=M:
            isInc=0
        #改变PWM值
        if isInc: #PWM增大，功率减小
            PWM_MOS+=1
        else:     #PWM增小，功率减大
            PWM_MOS-=1
        #改变频率
        i-=1
        if i==0:
            i=n//(T_ms*T_ms)
            T_ms-=1
            print(i)
        #追加
        seq.append((t, PWM_MOS, T_ms))
        #延时
        t+=T_ms
    return seq

seq = logf_scan(2000, 4000000, 125, 225)
t, pwm, T_ms = np.array(seq).transpose()
t_min = t/60000
plt.xlabel('time(min)')
plt.plot(t_min, pwm, c='y')
plt.ylabel('PWM value')
plt.twinx()
plt.plot(t_min, T_ms, c='b')
plt.yscale('log')
plt.ylabel('period(s)')
plt.show()
